package com.fjec.market.controller;

import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import com.fjec.common.util.SequenceIdGenerator;
import com.fjec.market.service.IShopService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户综合服务类
 */
@RestController
public class ShopController {

    @Resource
    private IShopService shopService;

    @Autowired
    private SequenceIdGenerator idGenerator;

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/y/shop/phone")
    public JsonResult getShopPhone() {
        return shopService.getShopPhone();
    }

    /**
     * 修改用户电话
     *
     * @param newPhone   修改后的电话号码
     * @param verifyCode 短信验证码
     * @return
     */
    @PostMapping("/y/shop/phone/update")
    public JsonResult updateShopPhone(String newPhone, String verifyCode) {
        return shopService.updateShopPhone(newPhone, verifyCode);
    }

    /**
     * 修改是否允许打电话
     *
     * @param allowPhone
     * @return
     */
    @PostMapping("/y/shop/phone/allow")
    public JsonResult updateShopAllowPhone(Integer allowPhone) {
        return shopService.updateShopAllowPhone(allowPhone);
    }

    /**
     *
     */
    @GetMapping("/y/shop/get/all4/status")
    public JsonResult getAll4Status() {
        Map<String, Object> result = shopService.getAll4Status();
        return JsonResult.success().data(result).build();

    }

    /**
     * 根据userId查询店铺进件状态
     * 0=未注册商铺
     * 1=基本信息
     * 2=已完善信息
     * 3=已进件
     *
     * @param userId
     * @return
     */
    @GetMapping("/rpc/get/shop/regStatus")
    public Integer rpcGetShopRegStatusByUserId(@RequestParam Long userId) {
        Map<String, Object> resultMap = shopService.getShopRegStatusByUserId(userId);
        return MapUtils.getInteger(resultMap, "regStatus", 0);
    }

    /**
     * 根据userId查询店铺进件状态
     * RegStatus 进件状态
     * 0=未注册商铺
     * 1=基本信息
     * 2=已完善信息
     * 3=已进件
     * <p>
     * DepoStatus 保证金状态
     * 0=未缴纳
     * 1=已缴纳
     * 2=申请退回
     * 3=已退回
     *
     * @return
     */
    @GetMapping("/y/get/shop/regStatus")
    public JsonResult getShopRegStatusByUserId() {
        Map<String, Object> resultMap = shopService.getShopRegStatusByUserId(AccountInfo.getUserId());
        return JsonResult.success()
                .data(resultMap)
                .build();
    }

    /**
     * 首页显示按钮
     *
     * @return
     */
    @GetMapping("/y/main/button/display")
    public JsonResult mainButtonDisplay() {
        return shopService.mainButtonDisplay();
    }

    @GetMapping("/n/id/generate")
    public Long generateId() {
        return idGenerator.nextId();
    }

    /**
     * 申请退回保证金
     *
     * @return
     */
    @PostMapping("/y/apply/refund/deposit")
    public JsonResult applyRefundDeposit() {
        int code = shopService.applyRefundDeposit();
        if (code == 1)
            return JsonResult.SUCCESS;
        else
            return JsonResult.FAILURE;
    }
}
